package com.tudouji.project.flow.controller;

import com.flow.service.pojo.BasePojo;
import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.bill.domain.Recharge;
import com.tudouji.project.bill.domain.Sellform;
import com.tudouji.project.bill.domain.Transferapply;
import com.tudouji.project.bill.mapper.RechargeMapper;
import com.tudouji.project.bill.mapper.SellformMapper;
import com.tudouji.project.bill.mapper.TransferapplyMapper;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.mapper.CustomerMapper;
import com.tudouji.project.farepay.domain.FarepayApply;
import com.tudouji.project.farepay.mapper.FarepayApplyMapper;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.harvest.domain.Freightorder;
import com.tudouji.project.harvest.domain.Procurementcontract;
import com.tudouji.project.harvest.mapper.FreightorderMapper;
import com.tudouji.project.harvest.mapper.ProcurementcontractMapper;
import com.tudouji.project.pay.domain.Payinfo;
import com.tudouji.project.pay.mapper.PayinfoMapper;
import com.tudouji.project.produce.domain.Materialretapply;
import com.tudouji.project.produce.domain.Pickinglist;
import com.tudouji.project.produce.mapper.MaterialretapplyMapper;
import com.tudouji.project.produce.mapper.PickinglistMapper;
import com.tudouji.project.purchasebill.domain.Purchasebill;
import com.tudouji.project.purchasebill.service.IPurchasebillService;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStoreroomService;
import com.tudouji.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaxl
 * @version v1.0
 * @ClassName FlowOperationController
 * @Descrption
 * @date 2022/4/24 14:52
 */
@Slf4j
@RestController
@RequestMapping("/operation")
public class FlowOperationController extends BaseController {

    @Autowired
    private FlowProcessUtil processUtil;
    @Autowired
    private FlowProcessProxy flowProcessProxy;

    @Autowired
    private IPurchasebillService purchasebillService;

    @Autowired
    private SellformMapper sellformMapper;
    @Autowired
    private IStoreroomService storeroomService;

    @Autowired
    private TransferapplyMapper transferapplyMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PickinglistMapper pickinglistMapper;
    @Autowired
    private MaterialretapplyMapper materialretapplyMapper;
    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private FarepayApplyMapper farepayApplyMapper;
    @Autowired
    private ProcurementcontractMapper procurementcontractMapper;
    @Autowired
    private FreightorderMapper freightorderMapper;
    @Autowired
    private PayinfoMapper payinfoMapper;
    /**
     * 流程消息回复
     */
    @Log(title = "消息回复", businessType = BusinessType.INSERT)
    @PostMapping("/reply")
    public AjaxResult reply(@RequestParam(value="fileIds") String fileIds, Long orderId, Long flowId, String content){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        BasePojo base=null;
        if(flowId.equals(new Long(26))){
            Purchasebill purchasebill = purchasebillService.selectPurchasebillById(orderId);
            base=purchasebill;
            Storeroom storeroom = storeroomService.selectStoreroomByGuid(purchasebill.getSrguid());
            base.setOrgId(storeroom.getId());
            base.setPlatformId(storeroom.getId());
        }else if(flowId.equals(new Long(28))){
            Transferapply transferapply = transferapplyMapper.selectTransferapplyByNewId(orderId);
            base=transferapply;
            Storeroom storeroom = storeroomService.selectStoreroomByGuid(transferapply.getTosrguid());
            base.setOrgId(storeroom.getId());
            Storeroom storeroomFrom = storeroomService.selectStoreroomByGuid(transferapply.getFromsrguid());
            base.setOrgId(Long.valueOf(storeroomFrom.getId()));
            base.setPlatformId(storeroom.getId());
            if(transferapply.getTosrguid()!=null){
                Storeroom toStoreroom = storeroomService.selectStoreroomByGuid(transferapply.getTosrguid());
                base.setToOrgId(Long.valueOf(toStoreroom.getId()));
                base.setToPlatformId(toStoreroom.getId());
            }
        }else if(flowId.equals(new Long(29))){
            Customer customer = customerMapper.selectCustomerByNewId(orderId);
            base=customer;
        }else if(flowId.equals(new Long(30))){
            Pickinglist pickinglist = pickinglistMapper.selectPickinglistByNewId(orderId);
            base=pickinglist;
        }else if(flowId.equals(new Long(31))){
            Materialretapply apply = materialretapplyMapper.selectMaterialretapplyByNewId(orderId);
            base = apply;
        }else if(flowId.equals(new Long(34)) || flowId.equals(new Long(35)) ||flowId.equals(new Long(36))){
            Sellform apply = sellformMapper.selectSellformById(orderId);
            base = apply;
            base.setPlatformId(apply.getStrmId());
        }else if(flowId.equals(new Long(37))){
            Recharge apply = rechargeMapper.selectRechargeByNewId(orderId);
            base = apply;
        }else if(flowId.equals(new Long(38))){
            FarepayApply apply = farepayApplyMapper.selectFarepayApplyById(String.valueOf(orderId));
            base = apply;
        }else if(flowId.equals(new Long(39))){
            Procurementcontract apply = procurementcontractMapper.selectProcurementcontractByNewId(orderId);
            base = apply;
        }else if(flowId.equals(new Long(40))){
            Freightorder apply = freightorderMapper.selectFreightorderByAutoId(orderId);
            base = apply;
            base.setPlatformId(apply.getStrid());
        }else if(flowId.equals(new Long(41))){
            Payinfo apply = payinfoMapper.selectPayinfoByAutoId(orderId);
            base = apply;
            base.setPlatformId(apply.getStorermId());
        }else if(flowId.equals(new Long(42))){
            Payinfo apply = payinfoMapper.selectPayinfoByAutoId(orderId);
            base = apply;
            base.setPlatformId(apply.getStorermId());
        }
        FlowResult result = flowProcessProxy.comment(flowId,fileIds,content,user,base);
        if(result.getMsg().equals(ResultParam.SUCCESS)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("系统错误");
        }
    }
}
